//
//  MangerCentre.m
//  BCManagerVersion
//
//  Created by 吴 崧 on 14-3-4.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "MangerCentre.h"
#import <objc/runtime.h>
#import "UserModel.h"
#define OBJC_STRINGIFY(x) @#x

//decode macro
#define decodeObject(x) x = [aDecoder decodeObjectForKey:OBJC_STRINGIFY(x)]
#define decodeBool(x) x = [aDecoder decodeBoolForKey:OBJC_STRINGIFY(x)]
#define decodeInteger(x) x = [aDecoder decodeIntegerForKey:OBJC_STRINGIFY(x)]
#define decodeFloat(x) x = [aDecoder decodeFloatForKey:OBJC_STRINGIFY(x)]
//encode macro
#define encodeObject(x) [aCoder encodeObject:x forKey:OBJC_STRINGIFY(x)]
#define encodeFloat(x) [aCoder encodeFloat:x forKey:OBJC_STRINGIFY(x)]
#define encodeInteger(x) [aCoder encodeInteger:x forKey:OBJC_STRINGIFY(x)]
#define encodeBool(x) [aCoder encodeBool:x forKey:OBJC_STRINGIFY(x)]

//Set Value For Key


#define EMPTY_NUMBER @(0)
#define EMPTY_ARRAY @[]
#define USERDEFAULT [NSUserDefaults standardUserDefaults]

static MangerCentre *sharedInstance = nil;
@implementation MangerCentre





+(instancetype)sharedManager
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSData *managerData = [USERDEFAULT objectForKey:USER_INFO_FLAG];
        if (managerData != nil) {
            sharedInstance = (MangerCentre *)[NSKeyedUnarchiver unarchiveObjectWithData:managerData];
        }else
        {
            sharedInstance = [[MangerCentre alloc] init];
        }
        
        
        [sharedInstance save];
    });
    return sharedInstance;
}

-(instancetype)init
{
    
    self = [super init];
    if (self) {
        [self setDefaultData];
        
        
    }
    return self;
}

-(void)setDefaultData
{
    
    _auth = EMPTY_STRING;
    _uid = EMPTY_NUMBER;
    _passport = EMPTY_STRING;
    _name = EMPTY_STRING;
    _avatar = @"userAvatar_placeholder";
    _tAvatar = EMPTY_STRING;
    _iphoneId = EMPTY_STRING;
    _register_time = EMPTY_STRING;
    _score = EMPTY_NUMBER;
    _verified = EMPTY_NUMBER;
    _gender = EMPTY_NUMBER;
    _tel = EMPTY_STRING;
    _modified_time = EMPTY_STRING;
    _bankMain = EMPTY_STRING;
    _bankSub1 = EMPTY_STRING;
    _bankSub2 = EMPTY_STRING;
    _workType = [NSMutableArray arrayWithCapacity:3];
    _workTime = [MangerCentre workTimes][0];
    _workTypeString = EMPTY_STRING;
    _city = EMPTY_STRING;
    _cityName = EMPTY_STRING;
    
    _didLogin = NO;
    _canPush = YES;
    _canAlert = YES;
    _canSound = YES;
    _canVibrator = YES;
    _address = EMPTY_STRING;

    [self save];
}


-(void)setAuth:(NSString *)auth
{
    if (auth == nil) {
        [self setDefaultData];
        
    }else if(![_auth isEqualToString:auth]&&![auth isEqualToString:EMPTY_STRING])
    {
        _auth = auth;
        _didLogin = YES;
        [self save];
    }
}


-(BOOL)save
{
    NSData *myEncodedObj = [NSKeyedArchiver archivedDataWithRootObject:self];
    [USERDEFAULT setObject:myEncodedObj forKey:USER_INFO_FLAG];
    BOOL sync = [USERDEFAULT synchronize];
    return sync;
}

#pragma mark ---Custom decode/encode
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        
        decodeObject(_auth);
        decodeObject(_uid);
        decodeObject(_passport);
        decodeObject(_age);
        decodeObject(_name);
        decodeObject(_avatar);
        decodeObject(_tAvatar);
        decodeObject(_iphoneId);
        decodeObject(_register_time);
        
        
        decodeObject(_score);
        decodeObject(_verified);
        decodeObject(_gender);
        decodeObject(_tel);
        
        
        decodeObject(_modified_time);
        decodeObject(_bankMain);
        decodeObject(_bankSub1);
        decodeObject(_bankSub2);
        
        decodeObject(_workType);
        _workType = [NSMutableArray arrayWithArray:_workType];
        decodeObject(_workTime);
        decodeObject(_workTypeString);
        
        
        decodeObject(_city);
        decodeObject(_cityName);
        
        decodeBool(_didLogin);
        decodeBool(_canPush);
        decodeBool(_canAlert);
        decodeBool(_canSound);
        decodeBool(_canVibrator);
        decodeObject(_address);

    }
    return self;
}



-(void)encodeWithCoder:(NSCoder *)aCoder
{
    
    encodeObject(_auth);
    encodeObject(_uid);
    encodeObject(_passport);
    encodeObject(_age);
    encodeObject(_name);
    encodeObject(_avatar);
    encodeObject(_tAvatar);
    encodeObject(_iphoneId);
    encodeObject(_register_time);
    
    
    encodeObject(_score);
    encodeObject(_verified);
    encodeObject(_gender);
    encodeObject(_tel);
    
    
    encodeObject(_modified_time);
    encodeObject(_bankMain);
    encodeObject(_bankSub1);
    encodeObject(_bankSub2);
    
    encodeObject(_workType);
    encodeObject(_workTime);
    encodeObject(_workTypeString);
    
    
    encodeObject(_city);
    encodeObject(_cityName);
    
    encodeBool(_didLogin);
    encodeBool(_canPush);
    encodeBool(_canAlert);
    encodeBool(_canSound);
    encodeBool(_canVibrator);
    encodeObject(_address);
}


+(void)reloadCache
{
  
        sharedInstance = (MangerCentre *)[NSKeyedUnarchiver unarchiveObjectWithData:[USERDEFAULT objectForKey:USER_INFO_FLAG]];

}

-(void)configWithModel:(NSDictionary *)model
{
    [self setValuesForKeysWithDictionary:model];
}

-(void)setWorkTime:(NSDictionary *)workTime
{
    _workTime = workTime;
    
}

- (void)setWorkTimeString:(NSString *)workTimeString
{
    NSArray* worktimes = [MangerCentre workTimes];
    [worktimes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[@"name"] isEqualToString:workTimeString]) {
            _workTime = obj;
            return;
        }
    }];
}

- (void)setWorkTimeId:(NSNumber *)workTimeId
{
    NSArray* worktimes = [MangerCentre workTimes];
    [worktimes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj[@"id"] isEqualToNumber:workTimeId]) {
            _workTime = obj;
            return;
        }
    }];
}

- (NSString*)workTimeString
{
    return _workTime[@"name"];
}

- (NSNumber*)workTimeId
{
    return _workTime[@"id"];
}


-(void)setWorkType:(NSMutableArray *)workType
{
    _workType = workType;
    if (workType.count > 0) {
         _workTypeString = [self.workType componentsJoinedByString:@"\n"];
    }
}


- (NSString*)workTypeString
{
    return [self.workType componentsJoinedByString:@"\n"];
}

- (NSString*)genderString
{
    if (self.gender.intValue == 0) {
        return @"男";
    }
    
    return @"女";
}

- (void)setGenderString:(NSString*)value
{
    if ([value isEqualToString:@"男"]) {
        self.gender = @0;
    }
    self.gender = @1;
}

- (NSString*)workType1
{
    if (self.workType.count > 0) {
        return self.workType[0];
    }
    return EMPTY_STRING;
}

- (void)setWorkType1:(NSString *)workType1
{
    self.workType = [NSMutableArray arrayWithArray:_workType];
    self.workType[0] = workType1;
}

- (NSString*)workType2
{
    if (self.workType.count > 1) {
        return self.workType[1];
    }
    return EMPTY_STRING;
}

- (void)setWorkType2:(NSString *)workType2
{
    self.workType = [NSMutableArray arrayWithArray:_workType];
    self.workType[1] = workType2;
}

- (NSString*)workType3
{
    if (self.workType.count > 2) {
        return self.workType[2];
    }
    return EMPTY_STRING;
}

- (void)setWorkType3:(NSString *)workType3
{
    self.workType = [NSMutableArray arrayWithArray:_workType];
    self.workType[2] = workType3;
}

+ (NSArray*)workTimes
{
    //1 三年以内， 2 三到五年， 3，五到八年， 4， 八年以上, 0, 未填写
    return @[
             @{@"name" : @"未填写",
               @"id" : @0},
             @{@"name" : @"三年以内",
               @"id" : @1},
             @{@"name" : @"三到五年",
               @"id" : @2},
             @{@"name" : @"五到八年",
               @"id" : @3},
             @{@"name" : @"八年以上",
               @"id" : @4},
             ];
}
@end
